<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">

      <el-form-item label="收信人" required>
        <el-select v-if="isRoleAdmin" v-model="form.to_user_id" placeholder="请选择收信人" style="width: 100%">
          <el-option v-for="u in userList" :key="u.id" :label="u.patient_id + '(' + u.patient_name + ')'"
                     :value="u.id"></el-option>
        </el-select>
        <el-select v-else v-model="form.to_user_id" placeholder="请选择收信人" style="width: 100%">
          <el-option v-for="u in adminList" :key="u.id" :label="u.account" :value="u.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="留言内容" required>
        <el-input type="textarea" v-model="form.message"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">发送留言</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getPatientList} from "@/api/patient";
import {addMessage} from "@/api/message";
import {getAdminList} from "@/api/users";

export default {
  name: "sendMessage",
  data() {
    return {
      form: {
        from_user: "",
        from_user_id: "",
        to_user_id: null,
        to_user: null,
        message: "",
        type: 0
      },
      userList: [
        {id: 1, patient_id: 'xxxxx', patient_name: 'xxx'}
      ],
      adminList: []
    }
  },
  computed: {
    isRoleAdmin() {
      // 从 sessionStorage 获取 role 值
      const role = sessionStorage.getItem('role');
      // 如果 role 值为 'admin'，则返回 true，否则返回 false
      return role === 'admin';
    }
  },
  methods: {
    getData() {
      getPatientList().then((res) => {
        this.userList = res.data
      })
      getAdminList().then((res) => {
        this.adminList = res.data
      })
    },
    onSubmit() {

      if (!this.form.to_user_id) {
        this.$message({
          message: "收信人不能为空！",
          type: "danger",
        });
      }
      if (!this.form.message) {
        this.$message({
          message: "留言内容不能为空！",
          type: "danger",
        });
      }

      this.form.from_user_id = JSON.parse(sessionStorage.getItem('userInfo')).id
      if (this.isRoleAdmin) {
        //  管理员给患者留言
        this.form.from_user = JSON.parse(sessionStorage.getItem('userInfo')).account
        this.form.to_user = this.userList.filter(user => user.id == this.form.to_user_id)[0].patient_id
        this.form.type = 0
      } else {
        // 患者给管理员留言
        let patientInfo = JSON.parse(sessionStorage.getItem('userInfo'))
        this.form.from_user = `${patientInfo.patient_id}(${patientInfo.patient_name})`
        this.form.to_user = this.adminList.filter(user => user.id == this.form.to_user_id)[0].account
        this.form.type = 1
      }
      addMessage(this.form).then(res => {
        if (res.code == 200) {
          this.$message({
            message: res.message,
            type: "success",
          });
          this.$emit('closeSendMessageModel');
        }
      })

    },
    cancel() {
      this.$emit('closeSendMessageModel');
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style scoped>

</style>
